﻿using BwTMusicModels;
using BwTServices.Album;
using BwTServices.Singer;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            AlbumServices album = new AlbumServices();
            SingerServices singer = new SingerServices();
            var albums = album.GetAllAlbum();
            for (int i = 0; i < albums.Count; i++)
            {
                var albumInfo = HttpAlbumInfo(albums[i].AlbumName, "post");
                var infos = JsonConvert.DeserializeObject<models>(albumInfo);
                if (infos.data != null)
                {
                    if (infos.data.info != null)
                    {
                        for (int j = 0; j < infos.data.info.Count; j++)
                        {
                            var singername = infos.data.info[j].singername.Replace("'","");
                            var singerinfo = singer.GetSingerInfo(singername);
                            if (singerinfo != null)
                            {
                                var singeId = singerinfo.SingerId;
                                var singerAlbum = album.GetSingerAlbumInfo(albums[i].AlbumId);
                                if (singeId == singerAlbum.SingerId)
                                {
                                    North_Album north_ = new North_Album();
                                    north_.AlbumDesc = infos.data.info[j].intro;
                                    north_.AlbumTime = infos.data.info[j].publishtime;
                                    north_.AlbumPic = infos.data.info[j].imgurl.Replace("{size}","240");
                                    north_.AlbumId = albums[i].AlbumId;
                                    album.ModifyAlbumInfo(north_);
                                    break;
                                }
                            }
                        }
                    }                  
                }
                Console.WriteLine($"完成第{i + 1}个,共{albums.Count}个");
            }

        }
        public static string HttpAlbumInfo(string name,string type)
        {
            string url = "http://msearch.kugou.com/api/v3/search/album?version=9108&iscorrection=1&highlight=em&plat=0&keyword=" + name + "&pagesize=50&page=1&sver=3&with_res_tag=3";
            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//webrequest请求api地址  
            request.Accept = "text/html,application/xhtml+xml,*/*";
            request.ContentType = "application/json";
            request.Method = type.ToUpper().ToString();//get或者post  
            byte[] buffer = encoding.GetBytes("{}");
            request.ContentLength = buffer.Length;
            request.GetRequestStream().Write(buffer, 0, buffer.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }
    }
}
